<template>
	<view class="tcr-activity">
		<view class="tcr-activity-item">
			<view class="tcr-activity-item-title">任务1：每日签到获取积分</view>
			<view class="tcr-activity-item-count">积分 +2</view>
			<view class="tcr-activity-item-btns">
				<button v-if="canPunchDay" @click="handlePunchDay" style="margin: 0 20rpx;" type="default" size="mini">签到</button>
				<text v-else style="font-size: 28rpx; color: #666666;">已签到</text>
			</view>
		</view>
		<view class="tcr-activity-item">
			<view class="tcr-activity-item-title">任务2：观看激励视频获取积分</view>
			<view class="tcr-activity-item-count">积分 +10</view>
			<view class="tcr-activity-item-can">今日剩余次数： {{ canUseCount }}</view>
			<view class="tcr-activity-item-btns">
				<button v-if="canViewVideo" :disabled="countSeconds > 0" @click="handleOpenVideoAdv" style="margin: 0 20rpx;" type="default" size="mini"><text v-if="countSeconds > 0">({{ countSeconds }}秒)</text>观看激励视频广告获取积分</button>
				<text v-else style="font-size: 28rpx; color: #666666;">今日完成任务数量已达上限，明天再试</text>
			</view>
		</view>
		<ad-rewarded-video ref="adRewardedVideo" adpid="1460162852" :preload="true" :loadnext="true" :disabled="true"
			v-slot:default="{loading, error}" @load="onadload" @close="onadclose" @error="onaderror">
			<view class="ad-error" v-if="error">{{error}}</view>
		</ad-rewarded-video>
	</view>
</template>

<script>
	import apis from '../../apis/apis.js';
	
	export default {
		data() {
			return {
				isLoading: false,
				canViewVideo: false,
				canPunchDay: false,
				openAdv: true,
				countSeconds: 0,
				canUseCount: 0,
			};
		},
		onReady() {
			this.isLoading = true;
		},
		onLoad() {
			this.loadActivityInfo();
		},
		methods: {
			/** 加载激励视频观看数量信息 */
			loadActivityInfo() {
				this.$http.get({
					url: apis.getTodayActivity
				}).then(res => {
					if (res.code === 200) {
						const count = res.data.spAdv;
						if (count >= 5) {
							this.canViewVideo = false;
						} else {
							this.canViewVideo = true;
							this.canUseCount = 5 - count;
						}
						// 每日签到
						if (res.data.punchDay == 1) {
							this.canPunchDay = false;
						} else {
							this.canPunchDay = true;
						}
					}
				}).catch(err => {
					console.log('调用接口失败：', err);
				})
			},
			onadload(e) {
				this.isLoading = false;
			},
			/** 加载激励视频广告 */
			handleOpenVideoAdv() {
				if (!this.openAdv) {
					uni.showToast({
						title: '暂未开放，敬请期待',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (this.isLoading) {
					return
				}
				this.$refs.adRewardedVideo.show();
			},
			onadclose(e) {
				const detail = e.detail
				// 用户点击了【关闭广告】按钮
				if (detail && detail.isEnded) {
					// 正常播放结束
					console.log("onadclose 正常" + detail.isEnded);
					this.addPoints();
				} else {
					// 播放中途退出
					console.log("onadclose 不正常" + detail.isEnded);
				}
				this.countSeconds = 10;
				const itv = setInterval(() => {
					this.countSeconds = this.countSeconds - 1;
					if (this.countSeconds === 0) {
						clearInterval(itv);
					}
				}, 1000);
			},
			onaderror(e) {
				// 广告加载失败
				console.log("onaderror: ", e.detail);
			},
			addPoints() {
				this.$http.post({
					url: apis.addActivityPoints,
					data: {},
				}).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: '积分获取成功',
							icon: 'none',
							duration: 1500
						});
						this.loadActivityInfo();
					}
				}).catch(err => {
					console.log('调用接口失败：', err);
				});
			},
			handlePunchDay() {
				this.$http.post({
					url: apis.addPunchPoints,
					data: {},
				}).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: '签到成功',
							icon: 'none',
							duration: 1500
						});
						this.loadActivityInfo();
					}
				}).catch(err => {
					console.log('调用接口失败：', err);
				});
			}
		}
	}
</script>

<style lang="scss">
	
	.tcr-activity {
		box-sizing: border-box;
		
		&-item {
			margin: 20rpx;
			border-radius: 20rpx;
			box-shadow: 0 2px 8px 0 rgba(0,0,0,0.2);
			padding: 20rpx;
			
			&-title {
				font-size: 32rpx;
				color: #333333;
			}
			
			&-count {
				font-size: 28rpx;
				margin-top: 10rpx;
				color: #1da100;
			}
			
			&-can {
				font-size: 28rpx;
				margin-top: 10rpx;
				color: #c30a0a;
			}
			
			&-btns {
				text-align: right;
				margin-top: 20rpx;
			}
		}
	}

</style>
